// pages/measureregister/measureRegister.js
var util = require('../../utils/util.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hiddenmodalput: true,//是否隐藏模态框
    currentOption: {},//当前编辑的选项
    tempValue: "",//文本框的临时内容
    monitorData: [],//列表数据
    userInfo: null,//用户信息，包含昵称/头像等相关信息
    tel: '',
    mtype:'',//分类 自测/监测
    dayno:'',//第几天
    currentTime:'',
    focus: false,
    offline: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    //设置基础信息
    that.userbaseinfo()

    //获取登记项列表
    const url = "/apid/r/125";
    //获取登记项的值,根据类型判断是126/127
    var detailUrl = "";
    //获取集中治疗日常监测结果
    const murl = "/apid/r/126";
    //获取居家治疗日常自测结果
    const mhurl = "/apid/r/127";
    //病人id
    const patientid = app.globalData.patientid;
    //获取检测指标列表
    util.DoCommand(url, "patientid=" + patientid, null, function (data) {
      if(data.length>0){
        var datalist = data;
        var mtype='';
        if (data[0].type=='home'){
          mtype ='居家治疗期';
        }
        else{
          mtype = '集中治疗期';
        }
        var myDate = new Date();//获取系统当前时间
        var currentTime = myDate.getHours() + ":" + myDate.getMinutes();

        that.setData({
          monitorData: data,
          mtype: mtype,
          dayno:'第'+data[0].dayno+'天',
          currentTime: currentTime
        });
        var type = data[0].type;
        if (type == 'monitor') {
          detailUrl = murl;
        }
        else if (type == "home") {
          detailUrl = mhurl;
        }
      }
      else{
        //没有检测指标
        util.DoCommand("/apid/r/147", "patientid=" + patientid, null, function (daydata) {
          that.setData({
            dayno: '第' + daydata[0].dayno + '天'
          });
        }, null, that)
        return;
      }
     
      //获取集中治疗日常监测结果
      util.DoCommand(detailUrl, "patientid=" + patientid, null, function (mdata) {
        if (!mdata) {
          //"没有家居记录"
          //插入一条数据
          var newid = util.NewGuid();
          var addurl = "";
          if (type == 'monitor') {
            addurl = "/apid/c/128";
          }
          else if (type == "home") {
            addurl = "/apid/c/129";
          }
          util.DoCommand(addurl, "id=" + newid + "&parentid=" + patientid, null, function () { }, null, that)
        }
        else {
          //如果已经记录过则，赋值value
          for (var i in datalist) {
            var _tempkey = datalist[i].monitorType;
            var _type = datalist[i].type;
            for (var key in mdata) {
              if (key == _tempkey){
                if (mdata[key] != '0' && mdata[key]!='0.0')
                datalist[i].value = mdata[key]
              }
            }
          }
        }
        that.setData({
          monitorData: datalist
        });
      }, null, that)
    }, null, that)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //同步input内容到数据中
  bindKeyInput: function (e) {
    this.setData({
      tempValue: e.detail.value
    })
  },
  //点击按钮弹出指定的hiddenmodalput弹出框  
  modalinput: function (e) {
    const index = e.currentTarget.dataset.index;
    const currentData = this.data.monitorData[index];
    const data = currentData.value;
    this.setData({
      hiddenmodalput: !this.data.hiddenmodalput,
      currentOption: currentData,
      focus:true,
      tempValue: data
    })
  },
  //取消按钮  
  cancel: function () {
    this.setData({
      hiddenmodalput: true,
      tempValue: '',
      focus:false
    });
  },
  //确认  
  confirm: function (e) {
    var id = util.NewGuid();
    const that=this;
    that.setData({
      focus:true
    });
    //病人id
    const patientid = app.globalData.patientid;
    //当前输入的值
    const optionValue = that.data.tempValue;
    //获取当前指标项code
    const monitorType = that.data.currentOption.monitorType;
    //获取当前指标项类型
    const mytype = that.data.currentOption.type;
    const url = "/apip/monitor/" + monitorType + "/" + optionValue + "/" + patientid + "/" + mytype;
    if (optionValue==''){
      wx.showToast({
        title: "您还未登记哦~",
        icon: "none"
      })
      return;
    }
    //提交数据
    util.DoCommand(url, null, null, function (data) {
      var olddata = that.data.monitorData;
      for(var i in olddata){
        if (olddata[i].monitorType == monitorType){
          olddata[i].value = optionValue;
        }
      }
      that.setData({
        hiddenmodalput: true,
        tempValue: '',
        monitorData:olddata,
        focus: true
      })
    }, null, that)
    
  },
  userbaseinfo:function(e){
    const accounttype = app.globalData.accounttype;
    const userinfo = app.globalData.userInfo;
    const openid = app.globalData.openid;
    const tel = app.globalData.tel
    this.setData({
      accounttype: accounttype,
      userInfo: userinfo,
      openid: openid,
      tel: tel
    })
  }
})